package com.cxdr.awsips.distribute.rule;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * 文件名分发策略
 * @author pugang
 *
 */
public class FileNameRule {
	private String id;
	private String filter_type;
	private String reg;
	public static final String INCLUDE_TYPE = "include";
	public static final String OUT_TYPE = "out";
	public FileNameRule(){
		
	}
	public FileNameRule(String filter,String reg){
		setFilter_type(filter);
		setReg(reg);
	}
	public FileNameRule(String id,String filter,String reg){
		setId(id);
		setFilter_type(filter);
		setReg(reg);
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getFilter_type() {
		return filter_type;
	}
	public void setFilter_type(String filter_type) {
		if(!INCLUDE_TYPE.equals(filter_type)&&!OUT_TYPE.equals(filter_type)){
			throw new RuntimeException("filter_type should be "+INCLUDE_TYPE+" or "+OUT_TYPE+" !");
		}
		this.filter_type = filter_type;
	}
	public String getReg() {
		return reg;
	}
	public void setReg(String reg) {
		this.reg = reg;
	}
	public boolean accept(String fileName){
		boolean accept = false;
		if(INCLUDE_TYPE.equals(filter_type)){
			accept =Pattern.matches(reg, fileName);
		}else if(OUT_TYPE.equals(filter_type)){
			accept =!Pattern.matches(reg, fileName);
		}
		return accept;
	}
	public String desc(){
		return "filter_type："+filter_type+" reg："+reg;
	}
	public static void main(String[] args) {
		FileNameRule role = new FileNameRule(FileNameRule.INCLUDE_TYPE,".*-REG_.*");
		System.out.println(role.accept("Z_SURF_C_BENN-REG_20120116214823_O_AWS_FTM.txt"));
	}

}
